<?php

namespace app\api\controller;

use app\admin\model\SpiderAiqiyi;
use app\admin\model\SpiderAiqiyiCollection;
use app\admin\model\SpiderAiqiyiLog;
use app\admin\model\SpiderTengxun;
use app\admin\model\SpiderTengxunLog;
use app\common\controller\Api;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Cookie\SetCookie;
use phpspider\core\requests;
use phpspider\core\selector;
use think\Request;
use function GuzzleHttp\Psr7\str;

/**
 * 测试接口
 */
class Test extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function test()
    {
        $url = "https://www.iqiyi.com/v_1yva5fekcxg.html";
        try{

            $content = requests::get($url);
            $preg = "|www.iqiyi.com\/([a-zA-Z0-9_]+?).html|";
            $text = selector::select($content, $preg,"regex");//获取albumId 用于获取热度

            $text = array_unique($text);

            dump($text);die;


        }catch (\GuzzleHttp\Exception\RequestException $e){
            // throw $e;
            $response = $e->getResponse();
            $code=$response->getStatusCode();
            $content = $response->getBody()->getContents();
            p($code);
            p($content);
            p($e->getMessage());die;
        }
    }

    public function test1(){
        $album_id = "4428456833070701";
        $parent_id = 1;
        $url = "https://pcw-api.iqiyi.com/albums/album/avlistinfo?aid=$album_id&page=1&size=100";
        try{
            $content = requests::get($url);
            $ar = json_decode($content,true);

            $epsodelist = $ar['data']['epsodelist']??[];
            $updateprevuelist = $ar['data']['updateprevuelist']??[];

            $arr = array_merge($epsodelist,$updateprevuelist);

            $list = [];
            foreach ($arr as $val){
                $list[]=[
                    "content_id"=>(string)$val['tvId'],
                    "collection"=>$val['name'],
                    "url"=>$val['playUrl'],
                    "parent_id"=>1
                ];
            }

            $model =  new \app\admin\model\SpiderAiqiyiCollection;
            $collection_list = $model->select();
            $content_ar = $collection_list?array_column($collection_list,"content_id"):[];

            foreach ($list as $list_val){
                if(!in_array($list_val['content_id'],$content_ar)){
                    $new_model = clone $model;
                    $new_model->save($list_val);
                }
            }


        }catch (\Exception $e){

        }
        echo "ok";die;
    }

    public function test2(){
        $model =  new \app\admin\model\SpiderAiqiyiCollection;
        $collection_list = $model->select();

        foreach ($collection_list as $val){
            p($val->content_id);
        }

        p(array_column($collection_list,'content_id'));
        p($collection_list);die;

    }

    public function test3(){
        $info = SpiderAiqiyiCollection::where("parent_id",1)->sum("comment_number");
        p($info);
        $date = date("Y-m-d");
        $model = SpiderAiqiyiLog::where("parent_id",1)->where("date",$date)->find();
        p($model);
        if(empty($model)){
            $model = new SpiderAiqiyiLog();
        }
        $data = [
            "comment_number"=>time(),
            "parent_id"=>1,
            "date"=>date("Y-m-d")
        ];

        $model->save($data);
    }

    //优酷抓包测试
    public function test4(){
        $url = "https://v.youku.com/v_show/id_XNTIwMTQxMDMyOA==.html";
        $url = "https://v.youku.com/v_show/id_XNTIwNDQ5Njk3Ng==.html?userId=".time();

        $url = "https://v.youku.com/v_show/id_XNTIwMzk5NTk0OA==.html?userId=".time();

      //  requests::set_cookie("cna", "QLJ6Gln1CgICAasPPVMW2NuC");
      //  requests::set_cookie("cna", "SSfzGNcGOUACAasPPnpvitRD");



        requests::set_useragent(
            [
            "Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/",
            "Opera/9.80 (Android 3.2.1; Linux; Opera Tablet/ADR-1109081720; U; ja) Presto/2.8.149 Version/11.10",
            "Mozilla/5.0 (Android; Linux armv7l; rv:9.0) Gecko/20111216 Firefox/9.0 Fennec/9.0"
            ]
        );
        requests::set_referer("https://www.youku.com");
        $ips = [
            "192.168.0.2",
            "192.168.0.3",
            "192.168.0.4"
            ];
        requests::set_client_ip($ips);

//        $content = requests::get("https://www.youku.com/");
        //$content = requests::get($url);
        $content = requests::get("https://www.youku.com");
        $cookies = requests::get_cookies("v.youku.com");
        p($cookies);
        if(strlen($content)<1000){
            $content = str_replace("<script>","< s c r i p t>",$content);
        }
        p("**********".$content."**************");die;

        $hot = selector::select($content, "//span[@class='video-heat-text']");//热度
        $gengxin = selector::select($content, "//div[@class='title-info']//span");//更新日期
        $arr = selector::select($content, "//a[@class='box-item']");//集数
        $jishu = 0;
        foreach ($arr as $arr_val){
            $a =  selector::select($arr_val, "//span[@class='label-text']");//集数
            $b =  selector::select($arr_val, "//span[@class='mark-text']");// vip,  预告
            if($a<1 && $b != "预告"){
                $a = $jishu + 1;
                $jishu = $a;
            }else{
                if($a>0 && $jishu<$a && $b != "预告"){
                    $jishu = $a;
                }
            }
            p("a:".$a."-----b:".$b);
        }
        p($jishu);
        p($arr);
        $ar = selector::select($content, "//div[@class='anthology-content']");//集数
        //评论数接口有加密验证，无法抓包
        $ar = $ar[0]??"";
        p($ar);
       // $ar = selector::select($ar, "/data-spm=\"(.+?)\"/","regex");//集数
       // p($ar);
        //$br = selector::select($ar, "#<a href=\"(.+?)\" data-spm=\"(.+?)\" .+?</a>#","regex");//集数
        $br = selector::select($ar, "#<a\s+href=\"(.+?)\"\s+data-spm=\"(.+?)\"\s+.+?</a>#","regex");//集数
        p($br);
        p($gengxin);
        p($hot);die;
    }
    //优酷抓包测试
    public function test41(){
        $url = "https://v.youku.com/v_show/id_XNTIwMTQxMDMyOA==.html";
        $url = "https://v.youku.com/v_show/id_XNTIwNDQ5Njk3Ng==.html?userId=".time();
        $url = "https://v.youku.com/v_show/id_XNTIwMzk5NTk0OA==.html?userId=".time();
        requests::set_cookie("cna", "QLJ6Gln1CgICAasPPVMW2NuC");

        requests::set_useragent("Mozilla/4.0 (compatible; MSIE 6.0; ) Opera/UCWEB7.0.2.37/28/");

        $ips = array(
            "192.168.0.2",
            "192.168.0.3",
            "192.168.0.4"
        );
        requests::set_client_ip($ips);


        $content = requests::get("http://101.35.219.22:88/index.php/api/test/test10");
        $cookies = requests::get_cookies("101.35.219.22:88");
        p($cookies);
        p($content);die;
        $hot = selector::select($content, "//span[@class='video-heat-text']");//热度
        $gengxin = selector::select($content, "//div[@class='title-info']//span");//更新日期
        $arr = selector::select($content, "//a[@class='box-item']");//集数
        $jishu = 0;
        foreach ($arr as $arr_val){
            $a =  selector::select($arr_val, "//span[@class='label-text']");//集数
            $b =  selector::select($arr_val, "//span[@class='mark-text']");// vip,  预告
            if($a<1 && $b != "预告"){
                $a = $jishu + 1;
                $jishu = $a;
            }else{
                if($a>0 && $jishu<$a && $b != "预告"){
                    $jishu = $a;
                }
            }
            p("a:".$a."-----b:".$b);
        }
        p($jishu);


        p($arr);

        $ar = selector::select($content, "//div[@class='anthology-content']");//集数

        //评论数接口有加密验证，无法抓包
        $ar = $ar[0]??"";
        p($ar);
        // $ar = selector::select($ar, "/data-spm=\"(.+?)\"/","regex");//集数

        // p($ar);

        //$br = selector::select($ar, "#<a href=\"(.+?)\" data-spm=\"(.+?)\" .+?</a>#","regex");//集数
        $br = selector::select($ar, "#<a\s+href=\"(.+?)\"\s+data-spm=\"(.+?)\"\s+.+?</a>#","regex");//集数




        p($br);

        p($gengxin);

        p($hot);die;
    }
    public function test42(){
        $url = "https://www.youku.com";
        try{
            $httpClient = new \GuzzleHttp\Client();
            $headers = ["Referer"=>"https://www.youku.com"];
            $cookies = [
                "cna"=>"SSfzGNcGOUACAasPPnpvitRD"
            ];

            $cookieJar = CookieJar::fromArray($cookies,"pbaccess.video.qq.com");
            $response = $httpClient->request("get",$url,[
                'headers'=>$headers,
//                "json"=>$data,
//                "form_params"=>$data,
                "cookies"=>$cookieJar
            ]);
            $content = $response->getBody()->getContents();
            if(strlen($content)<1000){
                $content = str_replace("<script>","< s c r i p t>",$content);
            }

            var_dump($content); die;
        }catch (\GuzzleHttp\Exception\RequestException $e){
            throw $e;
        }
    }

    //优酷抓包测试
    public function test5(){
        $url = "https://v.youku.com/v_show/id_XNTIwMTQxMDMyOA==.html";
        $url = "https://v.youku.com/v_show/id_XNTIwNDQ5Njk3Ng==.html?userId=".time();
        // $url = "https://v.youku.com/v_show/id_XNTIwMzgzOTA0OA==.html?userId=".time();
        $url = "https://v.youku.com/v_show/id_XNTIwMzk5NTk0OA==.html?userId=".time();
        $url = "https://www.baidu.com";
      //  requests::set_cookie("cna", "QLJ6Gln1CgICAasPPVMW2NuC; __ysuid=1644379807727lXh; isg=BFJSAA7VZIkB2ZgScoR1HGfqoBg0Y1b9sFe4Ahyrv4W6L_IpBPAIDYcHnwsTRM6V; tfstk=cH3FBjfOceLE3izY-yayF6BKcWLdaElnZNPbxK2rGJw11G07Us2Ce54nD5P5_Tqh.; l=eBLcPVz7LOtBOHrSBO5BEurza779ZIR44kPzaNbMiInca61hwHtbiNC32p1vkdtfgt1fDetzNzGSydLHR3fCOxDDBnV2ZDiInxf..; UM_distinctid=17edcacc2faceb-01d7e01abef94d8-4c3e237c-1fa400-17edcacc2fb65c; _m_h5_tk=048b75114a09a07c295fe65d228c225e_1650772147723; _m_h5_tk_enc=60ff5cc65485577e2c4d136b00af6fb1; P_ck_ctl=99CF05C97C59FED81341EB7C0761692C; __ayft=1650765005383; __aysid=1650765005383MWG; __arpvid=1650768141793qJ8VJ8-1650768141816; __ayscnt=1; __aypstp=10; __ayspstp=10; modalFrequency={\"UUID\":\"10\"}; xlly_s=1; __arycid=dh-3-00; __arcms=dh-3-00; __ayvstp=19; __aysvstp=19; redMarkRead=1");
        $content = requests::post($url);
        $cookies = requests::get_cookies("v.youku.com");
        p($cookies);
        //p($content);die;

        $hot = selector::select($content, "//span[@class='video-heat-text']");//热度

        $gengxin = selector::select($content, "//div[@class='title-info']//span");//更新日期


        $arr = selector::select($content, "//a[@class='box-item']");//集数

        $jishu = 0;
        foreach ($arr as $arr_val){
            $a =  selector::select($arr_val, "//span[@class='label-text']");//集数
            $b =  selector::select($arr_val, "//span[@class='mark-text']");// vip,  预告
            if($a<1 && $b != "预告"){
                $a = $jishu + 1;
                $jishu = $a;
            }else{
                if($a>0 && $jishu<$a && $b != "预告"){
                    $jishu = $a;
                }
            }
            p("a:".$a."-----b:".$b);
        }
        p($jishu);


        p($arr);

        $ar = selector::select($content, "//div[@class='anthology-content']");//集数

        //评论数接口有加密验证，无法抓包
        $ar = $ar[0]??"";
        p($ar);
        // $ar = selector::select($ar, "/data-spm=\"(.+?)\"/","regex");//集数

        // p($ar);

        //$br = selector::select($ar, "#<a href=\"(.+?)\" data-spm=\"(.+?)\" .+?</a>#","regex");//集数
        $br = selector::select($ar, "#<a\s+href=\"(.+?)\"\s+data-spm=\"(.+?)\"\s+.+?</a>#","regex");//集数




        p($br);

        p($gengxin);

        p($hot);die;
    }

    //腾讯抓包测试
    public function test6(){
        $url = "https://v.qq.com/x/cover/mzc00200iawcmn2/e00412i5tzl.html";
        try{
            $content = requests::get($url);
            $play = selector::select($content, "//div[@class='action_item action_count']");//播放量
            $update_date = selector::select($content, "//div[@class='mod_player_side']");//更新日期
            $play = selector::select($play,"//span[@class='icon_text']");
            $play = strip_tags($play);
            $update_date = selector::select($update_date, "//div[@class='player_hint']");// 每周六10点更新一集
            $preg = "/(每周.+?)'/";
            preg_match_all($preg,$update_date,$data);
            $update_date = $data[1][0]??"";

            $comment_id = selector::select($content, "#\"comment_id\"?:\"(.+?)\",#","regex");//  comment_id
            $cover_id = selector::select($content, "#\"cover_id\"?:\"(.+?)\",#","regex");  //  cover_id


            $url="https://video.coral.qq.com/article/".$comment_id."/commentnum";
            $json = requests::get($url);
            $json_ar = json_decode($json,true);
            p($json_ar);

            $comment_number = $json_ar['data']["commentnum"]??0;
            p($comment_id);
            p($cover_id);
            p($update_date);
            p($play);die;

            $update_data = [
                "play"=>(string)trim($play),
                "update_date"=>$update_date,
                "comment_number"=>0,
                "barrage_number"=>0,
                "current_collection"=>0
            ];

        }catch (\Exception $e){
            throw $e;
        }
    }

    public function test7(){
        $url = "https://pbaccess.video.qq.com/trpc.universal_backend_service.page_server_rpc.PageServer/GetPageData?video_appid=3000010&vplatform=2";

        $data = '{"page_params":{"req_from":"web","page_type":"detail_operation","page_id":"detail_page_series_list","cid":"mzc00200iawcmn2"},"has_cache":1}';
        $data = '{"page_params":{"req_from":"web","page_type":"detail_operation","page_id":"vsite_episode_list","cid":"mzc00200iawcmn2","vid":"e00412i5tzl","page_size":"100"}}';

        $data1 = [
            "page_params"=>[
                "req_from"=>"web",
                "page_type"=>"detail_operation",
                "page_id"=>"detail_page_series_list",
                "cid"=>"mzc00200iawcmn2",
                "vid"=>"e00412i5tzl",
                "page_size"=>"200"
            ],
        ];
        requests::set_cookie("vversion_name","8.2.95","pbaccess.video.qq.com");
        requests::set_header("Referer","https://v.qq.com/");
        //requests::$rawheaders = ["content-type"=>"application/json"];
        $content = requests::post($url,$data1);

        p($content);die;
    }

    public function test8(){
        $cid = "mzc00200r7x3tiz";
        $url = "https://pbaccess.video.qq.com/trpc.universal_backend_service.page_server_rpc.PageServer/GetPageData?video_appid=3000010&vplatform=2";
        $url = "https://pbaccess.video.qq.com/trpc.universal_backend_service.page_server_rpc.PageServer/GetPageData?video_appid=3000010&vplatform=2";
        $data = [
            "page_params"=>[
                "req_from"=>"web",
                "id_type"=>"1",
                "page_type"=>"detail_operation",
                "page_id"=>"vsite_episode_list",
                "cid"=>$cid,
              //  "vid"=>"e00412i5tzl",
                "page_size"=>"500"
            ],
        ];
        try{
            $httpClient = new \GuzzleHttp\Client();
            $headers = ["Referer"=>"https://v.qq.com/"];
            $cookies = [
                "vversion_name"=>"8.2.95"
            ];
            $cookieJar = CookieJar::fromArray($cookies,"pbaccess.video.qq.com");
            $response = $httpClient->request("post",$url,[
                'headers'=>$headers,
                "json"=>$data,
                "form_params"=>$data,
                "cookies"=>$cookieJar
                ]);
            $content = $response->getBody()->getContents();
            $data = json_decode($content,true);

            p($data);die;
            p($data["data"]["module_list_datas"][0]["module_datas"][0]["item_data_lists"]["item_datas"]);
            p($data);die;
            return $data;
        }catch (\GuzzleHttp\Exception\RequestException $e){
            throw $e;
        }
    }

    public function test9(){

       $a =  SpiderTengxunLog::where("parent_id",1)->find();
       p($a["play_number"]);
       p($a);die;

       $a =  strtotime("-1 day");

       p(date("Y-m-d H:i:s",$a));
       p($a);die;
        $str = "5.7亿次专辑播放";

        $number = (float)$str;
        if(strpos($str,"亿") !==false){
            $number = $number * 10000;
        }elseif(strpos($str,"万") !==false){
            $number = sprintf("%.2f",$number);
        }elseif(strpos($str,"千") !==false){
            $number = $number * 0.1;
            $number = sprintf("%.2f",$number);
        }elseif(strpos($str,"百") !==false){
            $number = $number * 0.01;
            $number = sprintf("%.2f",$number);
        }

        p($number);die;

    }

    public function test10(){

        dump($_SERVER);die;
    }

    public function test11(){

       $res =  \think\Log::write("362******".date("Y-m-d H:i:s"));

       p($res);die;
    }
}
